package cn.gok.oa.system.controller;


import cn.gok.oa.log.LogAnnotation;
import cn.gok.oa.system.dto.RoleDto;
import cn.gok.oa.system.dto.RoleListDto;
import cn.gok.oa.system.entity.TbRole;
import cn.gok.oa.system.service.ITbRoleService;
import cn.gok.oa.util.ResultData;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gok
 * @since 2022-08-04
 */
@RestController
@RequestMapping("/role")
public class TbRoleController {

    @Autowired
    ITbRoleService iTbRoleService;

    @RequestMapping(value = "/info",method = RequestMethod.GET)
    @LogAnnotation("获取角色列表")
    @ResponseBody
    public ResultData info(RoleListDto roleListDto){
        IPage<TbRole> iPage = iTbRoleService.info(roleListDto);
        return ResultData.page(iPage.getRecords(), iPage.getTotal());
    }

    @RequestMapping(value = "/addRole",method = RequestMethod.POST)
    @LogAnnotation("添加角色")
    public ResultData addRole(@RequestBody RoleDto roleDto, HttpServletRequest httpServletRequest){
        try {
            iTbRoleService.addRole(roleDto, httpServletRequest);
        } catch (RuntimeException e) {
            return ResultData.fail();
        } return ResultData.success();
    }


    @RequestMapping(value = "/deleteRole", method = RequestMethod.DELETE)
    @LogAnnotation("删除角色")
    public ResultData deleteRole(@Param("roleId") Integer roleId, HttpServletRequest httpServletRequest){
        try {
            iTbRoleService.deleteRole(roleId, httpServletRequest);
        } catch (RuntimeException e) {
            return ResultData.fail();
        } return ResultData.success();
    }

    @RequestMapping(value = "/updateRole", method = RequestMethod.PUT)
    @LogAnnotation("更新角色")
    public ResultData updateRole(@RequestBody RoleDto roleDto, HttpServletRequest httpServletRequest){
        try {
            iTbRoleService.updateRole(roleDto,httpServletRequest);
        } catch (RuntimeException e) {
            return ResultData.fail();
        } return ResultData.success();
    }




}
